//
//  ActiveController.m
//  Stock
//
//  Created by sun black on 11-11-27.
//  Copyright (c) 2011年 __MyCompanyName__. All rights reserved.
//

#import "ActiveController.h"

@implementation ActiveController

@synthesize tableView=_tableView;
@synthesize products;
@synthesize radarDateLabel;
@synthesize titleView;
@synthesize footerView;

-(void)requestFinishedWithResult:(id)aResult andUserinfo:(NSDictionary *)aUserinfo{
    NSString *action = [aUserinfo objectForKey:kASIHttpRequestUserinfoKey];
    if ([kQueryProducts isEqualToString:action]) {
        self.products = aResult;
        [self.tableView reloadData];
    }else if([kPurchaseProduct isEqualToString:action]){
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil message:@"购买成功" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        [alert release];
    }
    [self.view showHUDLoadingView:NO];
}
-(void)requestFinishedwithError:(NSError *)aError andUserinfo:(NSDictionary *)aUserinfo{
    [self.view showHUDLoadingView:NO];
}


-(void)purchaseProduct:(id)sender{
    UIButton *button = (UIButton *)sender;
    [self buy:button.tag];
}
-(void)buy:(NSUInteger)index{
    [self.view showHUDLoadingView:YES];
    //SKProduct *product = [products objectAtIndex:index];
    NSDictionary *product = [products objectAtIndex:index];
    [[StoreKitContext shareInstance] purchaseProduct:[product objectForKey:@"productId"]/*product.productIdentifier*/];
}
- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.navigationController.navigationBar.barStyle = UIBarStyleBlack;
    
    [self.titleView removeFromSuperview];
    [self.footerView removeFromSuperview];
    
    
    self.tableView.tableHeaderView = self.titleView;
    self.tableView.tableFooterView = self.footerView;
    
    [StoreKitContext shareInstance].delegate = self;
    
    NSMutableDictionary *dict1 = [NSMutableDictionary dictionary];
    [dict1 setObject:@"D_Card" forKey:@"productId"];
    [dict1 setObject:@"月卡" forKey:@"name"];
    [dict1 setObject:@"6" forKey:@"price"];
    /*
    NSMutableDictionary *dict2 = [NSMutableDictionary dictionary];
    [dict2 setObject:@"Season_Card" forKey:@"productId"];
    [dict2 setObject:@"季卡" forKey:@"name"];
    [dict2 setObject:@"18" forKey:@"price"];
    NSMutableDictionary *dict3 = [NSMutableDictionary dictionary];
    [dict3 setObject:@"Harf_Year_Card" forKey:@"productId"];
    [dict3 setObject:@"半年卡" forKey:@"name"];
    [dict3 setObject:@"30" forKey:@"price"];
    
    */
    
    //[[StoreKitContext shareInstance] queryProducts];
    self.products = [NSArray arrayWithObjects:dict1,nil];
    
    
    //[self.view showHUDLoadingView:YES];
    //[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(updateRadarDate) name:kNeedUpdateRadarDateNotification object:nil];
    
}
-(void)dealloc{
    self.titleView = nil;
    self.footerView = nil;
    self.tableView = nil;
    self.products = nil;
    [StoreKitContext shareInstance].delegate = nil;
    self.radarDateLabel = nil;
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [super dealloc];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    self.tableView = nil;
    self.titleView = nil;
    self.footerView = nil;
    self.products = nil;
    [[StoreKitContext shareInstance] cancleAllRequest];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return products.count?1:0;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return section==0?products.count:1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *CellIdentifier = @"RadarListCell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    if (cell == nil) {
        cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:CellIdentifier] autorelease];
        cell.textLabel.textColor = [UIColor whiteColor];
        cell.detailTextLabel.textColor = [UIColor whiteColor];
        cell.accessoryType = UITableViewCellAccessoryNone;
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    }
    
    if (indexPath.section==1) {
        cell.textLabel.text = @"升级到专业版";
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
    }else{
        //SKProduct *prodcut = [self.products objectAtIndex:indexPath.row];
        NSDictionary *product = [self.products objectAtIndex:indexPath.row]; 
        cell.textLabel.text = [product objectForKey:@"name"];//prodcut.localizedTitle;
        cell.detailTextLabel.text = [NSString stringWithFormat:@"¥%@",[product objectForKey:@"price"]];
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(250.0, 4.0, 60.0, 36.0)];
        [button setTitle:@"购买" forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [button addTarget:self action:@selector(purchaseProduct:) forControlEvents:UIControlEventTouchUpInside];
        button.tag = indexPath.row;
        cell.accessoryView = button;
        [button release]; 
    }
    NSString *imageName = @"cellbg2.png";
    if (indexPath.row%2==1) {
        imageName = @"cellbg1.png";
    }
    cell.backgroundView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:imageName]] autorelease];
    
    return cell;
}
/*
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
    return @"越狱用户若无法成功充值，请去AppStore免费下载安装";
}
- (NSString *)tableView:(UITableView *)tableView titleForFooterInSection:(NSInteger)section{
    return @"购买后将会自动续费，购买后将会自动续费，购买后将会自动续费，购买后将会自动续费，购买后将会自动续费，购买后将会自动续费，购买后将会自动续费，购买后将会自动续费，购买后将会自动续费，购买后将会自动续费，购买后将会自动续费，购买后将会自动续费，购买后将会自动续费，";
}*/
/*
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40.0;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 320.0, 40.0)];
    view.backgroundColor = [UIColor clearColor];
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(20.0,20.0, 280.0, 20.0)];
    label.text = section==0?@"购买":@"或者";
    label.backgroundColor = [UIColor clearColor];
    label.textColor = [UIColor whiteColor];
    [view addSubview:label];
    [label release];
    return [view autorelease];
}*/
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    /*if (indexPath.section==1&&indexPath.row==0) {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:ProVersionAppStoreLink]];
    }*/
    [self buy:indexPath.row];
    
}

@end
